#include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;

void *threadRoutine(void *args)
{
    int i = 0;
    int *data = new int[10];
    while (true)
    {
        cout << "新线程: " << (char *)args << " running" << endl;
        sleep(1);
    }
    cout << "new thread quit" << endl;
}
int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void *)"thread1");
    printf("%lu, %p\n", tid, tid);
    int count = 0;
    while (true)
    {
        cout << "main线程:"
             << " running... main tid: " << pthread_self() << endl;
        sleep(1);
        count++;
        if (count >= 5)
            break;
    }
    pthread_cancel(tid);
    cout << "pthread cancel:" << tid << endl;
    int *ret = nullptr;
    pthread_join(tid, (void **)&ret); // 默认会阻塞等待新线程退出
    cout << "main thread wait done ... main quit ...: new thead quit : "
         << (long long)ret << "\n ";
    sleep(5);

    return 0;
}